package com.guldan.thrall.web.controller;

import com.alibaba.fastjson.JSON;
import com.guldan.core.dubbo.client.ResponseDTO;
import com.guldan.core.response.ResponseJson;
import com.guldan.core.search.Pagination;
import com.guldan.jaina.dto.users.*;
import com.guldan.jaina.provider.IUserProvider;
import com.guldan.thrall.common.base.BaseController;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * Created by Silas.
 * Date: 2016/11/18
 * Time: 23:25
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController {

    @Autowired
    private IUserProvider iUserProvider;


    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseJson
    public
    @ResponseBody
    Pagination<UsersDTO> list(@RequestBody UsersSearchDTO usersSearchDTO) {
        logger.info("usersSearchDTO：" + JSON.toJSONString(usersSearchDTO));

        ResponseDTO<Pagination<UsersDTO>> response = iUserProvider.userListByPage(usersSearchDTO);
        return response.getData();
    }

    @RequestMapping(value = "/get", method = RequestMethod.POST)
    @ResponseJson
    public
    @ResponseBody
    UsersDTO get(@RequestBody UsersDTO dto) {
        ResponseDTO<UsersDTO> response = iUserProvider.userGet(dto.getUserId());
        return response.getData();
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseJson
    public
    @ResponseBody
    UsersDTO save(@RequestBody UsersDTO dto) throws Exception {
        ResponseDTO<UsersDTO> response = iUserProvider.userSave(dto);
        if (response.isSuccess())
            return response.getData();
        else {
            throw response.getException();
        }
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseJson
    public
    @ResponseBody
    Boolean delete(@RequestBody UsersDTO dto) {
        ResponseDTO<Boolean> response = iUserProvider.userDelete(dto.getUserId());
        return response.getData();
    }

    @RequestMapping(value = "/roleList", method = RequestMethod.POST)
    @ResponseJson
    public
    @ResponseBody
    List<UserRoleDTO> roleList(@RequestBody UsersDTO dto) {
        ResponseDTO<List<UserRoleDTO>> response = iUserProvider.userRoleList(dto.getUserId());
        return response.getData();
    }

    @RequestMapping(value = "/deleteRole", method = RequestMethod.POST)
    @ResponseJson
    public
    @ResponseBody
    List<UserRoleDTO> deleteRole(@RequestBody UserRoleDTO dto) {
        ResponseDTO<List<UserRoleDTO>> response = iUserProvider.userDeleteRole(dto.getUserId(), dto.getRoleId());
        return response.getData();
    }

    @RequestMapping(value = "/saveRole", method = RequestMethod.POST)
    @ResponseJson
    public
    @ResponseBody
    List<UserRoleDTO> saveRole(@RequestBody UserRoleSaveDTO dto) {
        ResponseDTO<List<UserRoleDTO>> response = iUserProvider.userSaveRole(dto);
        return response.getData();
    }

    @RequestMapping(value = "/updatePwd", method = RequestMethod.POST)
    @ResponseJson
    public
    @ResponseBody
    Boolean updatePwd(@RequestBody UserPwdUpdateDTO dto) {
        ResponseDTO<Boolean> response = iUserProvider.userUpdatePwd(dto);
        return response.getData();
    }
}
